<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <input type="text">
    <ul>
        <li></li>
    </ul>
    <script>
        var ipt = document.querySelector('input');
        var ul = document.querySelector('ul');
        ipt.oninput = function () {
            console.log(ipt.value);
            let val = ipt.value;

            //判断搜索框内容是否有内容,如果有内容出现联想,如果没有内容不提示联想
            if (val) {
                let s = document.createElement('script');
                s.src = `http://suggestion.baidu.com/su?cb=callback&wd=${val}`;
                document.body.appendChild(s);
            } else {
                ul.innerHTML = '';
            }

        }

        //函数接受参数
        function callback(res) {
            console.log(res.s);
            let str = ``;
            res.s.forEach(item => {
                str += `<li>${item}</li>`
            });
            ul.innerHTML = str;
        }



    </script>
</body>

</html>